<template>
    <div class="overall" @click="goToNewsDetails(newsContent.id)">
      <ul>
        <li class="img">
          <img :src="newsContent.img_src" :width="imgWidth">
        </li>
        <li class="title" :style="'width:'+imgWidth+'px'">
          {{newsContent.title}}
        </li>
        <li class="content" :style="'width:'+imgWidth+'px'">
          {{newsContent.content}}
        </li>
        <li class="icon">
          <van-icon name="arrow"/>
        </li>
      </ul>
    </div>
</template>

<script>
    export default {
        name: "theNews",
      computed:{
        title:{
          get:function () {
            let screenWidth=document.body.clientWidth;
            let wordNum=(screenWidth/16*2)-3;
            if(this.newsContent.title.length<wordNum){
              return this.newsContent.title
            }else {
              return this.newsContent.title.slice(0, (wordNum - 3)) + '...';
            }
          },
          set:function () {}
        },
        content:{
          get:function () {
            return this.newsContent.content.slice(0,(this.wordNum-3))+'...';
          },
          set:function () {}
        },
        lang:{
          get(){
            let storageData = window.sessionStorage.getItem('lang');
            if(this.$store.state.lang!==storageData&&storageData!=='null'){
              this.$store.dispatch('toChangeLang', storageData);    //同步操作
            }
            return this.$store.getters.getLang;
          },
          set(){}
        },
      },
      props:{
        newsContent:{},
        imgWidth:Number,
        wordNum:Number,
      },
      methods: {
        cut_str(str, len) {
          let char_length = 0;
          for (let i = 0; i < str.length; i++) {
            let son_str = str.charAt(i);
            encodeURI(son_str).length > 2 ? char_length += 1 : char_length += 0.5;
            if (char_length >= len) {
              let sub_len = char_length == len ? i + 1 : i;
              return str.substr(0, sub_len);
              break;
            }
          }
        },
        goToNewsDetails(id) {
          this.$router.push('/NewsDetails');
          this.$store.dispatch('toChangeNewsId',id);
          this.$store.dispatch('toChangeNowPage', '/NewsDetails');
          let pageName = '新闻详情';
          if (this.lang === 'en') {
            pageName = 'news details';
          }
          this.$store.dispatch('toChangeCurrentPageText', pageName);
        },
      },
    }
</script>

<style scoped>
.overall{
  width:100%;
  border-bottom: 1px solid #f5f5f5;
  background: #fff;
}
  .overall ul{
    text-align: left;
    padding:10px;
  }
  .overall ul .content{
    font-size:12px;
    color:#999999;
    line-height: 18px;
    overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;      /* 可以显示的行数，超出部分用...表示*/
    -webkit-box-orient: vertical;
  }
  .overall ul .title{
    font-size: 16px;
    padding-bottom: 5px;
    overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;      /* 可以显示的行数，超出部分用...表示*/
    -webkit-box-orient: vertical;
  }
.overall ul .icon{
  text-align: right;
  color:#999999;
  font-size:10px;
}
.van-icon{
  font-size:13px;
}
</style>
